package com.ruoyi.statistic.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.statistic.domain.StatisticStudentBaseInfo;
import com.ruoyi.statistic.domain.vo.*;
import com.ruoyi.statistic.mapper.StatisticStudentBaseInfoMapper;
import com.ruoyi.statistic.service.IStatisticStudentBaseInfoService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StatisticStudentBaseInfoServiceImpl implements IStatisticStudentBaseInfoService {


    @Resource
    private StatisticStudentBaseInfoMapper statisticStudentBaseInfoMapper;


    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 查询学生管理
     *
     * @param id 学生管理主键
     * @return 学生管理
     */
    @Override
    public StatisticStudentBaseInfo selectStatisticStudentBaseInfoById(Long id) {
        return statisticStudentBaseInfoMapper.selectStatisticStudentBaseInfoById(id);
    }

    /**
     * 查询学生管理
     *
     * @param code
     * @return
     */
    @Override
    public StatisticStudentBaseInfo selectStatisticStudentBaseInfoByCode(String code) {
        return statisticStudentBaseInfoMapper.selectStatisticStudentBaseInfoByCode(code);
    }

    /**
     * 查询学生管理列表
     *
     * @param statisticStudentBaseInfo 学生管理
     * @return 学生管理集合
     */
    @Override
    public List<StatisticStudentBaseInfo> selectStatisticStudentBaseInfoList(StatisticStudentBaseInfo statisticStudentBaseInfo) {
        return statisticStudentBaseInfoMapper.selectStatisticStudentBaseInfoList(statisticStudentBaseInfo);
    }

    /**
     * 查询学生管理列表
     *
     * @param statisticStudentListSearchVo
     * @return
     */
    @Override
    public List<StatisticStudentBaseInfo> selectStatisticStudentBaseInfoListBySearchVo(StatisticStudentListSearchVo statisticStudentListSearchVo) {
        List<StatisticStudentBaseInfo> list = statisticStudentBaseInfoMapper.selectStatisticStudentBaseInfoListBySearchVo(statisticStudentListSearchVo);
        return fillCounsellors(list);
    }


    /**
     * 查询学生管理列表（针对辅导员，返回全国系统上人数）
     * @param statisticStudentListSearchVo
     * @return
     */
    public List<StatisticStudentBaseInfo> selectCounsellorStatisticStudentBaseInfoListBySearchVo(StatisticStudentListSearchVo statisticStudentListSearchVo){
        List<StatisticStudentBaseInfo> list = statisticStudentBaseInfoMapper.selectCounsellorStatisticStudentBaseInfoListBySearchVo(statisticStudentListSearchVo);
        return fillCounsellors(list);
    }


    /**
     * 新增学生管理
     *
     * @param statisticStudentBaseInfo 学生管理
     * @return 结果
     */
    @Override
    public int insertStatisticStudentBaseInfo(StatisticStudentBaseInfo statisticStudentBaseInfo) {
        statisticStudentBaseInfo.setCreateTime(DateUtils.getNowDate());
        return statisticStudentBaseInfoMapper.insertStatisticStudentBaseInfo(statisticStudentBaseInfo);
    }

    /**
     * 修改学生管理
     *
     * @param statisticStudentBaseInfo 学生管理
     * @return 结果
     */
    @Override
    public int updateStatisticStudentBaseInfo(StatisticStudentBaseInfo statisticStudentBaseInfo) {
        statisticStudentBaseInfo.setUpdateTime(DateUtils.getNowDate());
        return statisticStudentBaseInfoMapper.updateStatisticStudentBaseInfo(statisticStudentBaseInfo);
    }

    /**
     * 批量删除学生管理
     *
     * @param ids 需要删除的学生管理主键集合
     * @return 结果
     */
    @Override
    public int deleteStatisticStudentBaseInfoByIds(Long[] ids) {
        return statisticStudentBaseInfoMapper.deleteStatisticStudentBaseInfoByIds(ids);
    }

    /**
     * 删除学生管理信息
     *
     * @param id 学生管理主键
     * @return 结果
     */
    @Override
    public int deleteStatisticStudentBaseInfoById(Long id) {
        return statisticStudentBaseInfoMapper.deleteStatisticStudentBaseInfoById(id);
    }

    /**
     * 更新学生带兵
     *
     * @param vo
     */
    @Transactional
    @Override
    public void batchUpdateLead(StatisticUpdateLeadVo vo) {
        List<StatisticStudentBaseInfo> query = statisticStudentBaseInfoMapper.selectStatisticStudentBaseInfoByIds(vo.getIds());
        for (StatisticStudentBaseInfo model : query) {
            statisticStudentBaseInfoMapper.updateStatisticStudentCounsellor(model.getCode(), vo.getCounsellorCode());
        }
    }

    /**
     * 批量更新系别专业
     * 系别和专业如果为空的话，默认不更新
     *
     * @param vo
     */
    @Transactional
    @Override
    public void batchUpdateDegree(StatisticUpdateDegreeVo vo) {
        List<StatisticStudentBaseInfo> query = statisticStudentBaseInfoMapper.selectStatisticStudentBaseInfoByIds(vo.getIds());
        for (StatisticStudentBaseInfo model : query) {
            if (!StringUtils.isEmpty(vo.getDepartment())) {
                statisticStudentBaseInfoMapper.batchUpdateDepartment(model.getCode(), vo.getDepartment());
            }
            if (!StringUtils.isEmpty(vo.getMajor())) {
                statisticStudentBaseInfoMapper.batchUpdateMajor(model.getCode(), vo.getMajor());
            }
        }
    }

    /**
     * 基础学生数据系别统计
     *
     * @param year
     * @return
     */
    @Override
    public List<BaseDepartmentVo> baseDepartmentCount(String year) {
        return statisticStudentBaseInfoMapper.baseDepartmentCount(year);
    }

    /**
     * 基础学生数据专业统计
     *
     * @param year
     * @return
     */
    @Override
    public List<BaseMajorVo> baseMajorCount(String year) {
        return statisticStudentBaseInfoMapper.baseMajorCount(year);
    }


    /**
     * 填充学生的辅导员信息
     *
     * @param list
     * @return
     */
    private List<StatisticStudentBaseInfo> fillCounsellors(List<StatisticStudentBaseInfo> list) {
        if (ObjectUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<String> counsellorCodes = list.stream().map(StatisticStudentBaseInfo::getCounsellorCode).distinct().collect(Collectors.toList());
        if (ObjectUtil.isEmpty(counsellorCodes)) {
            return list;
        }
        List<SysUser> sysUserList = sysUserMapper.selectUserByUserNames(counsellorCodes);
        for (StatisticStudentBaseInfo statisticStudentBaseInfo : list) {
            String code = statisticStudentBaseInfo.getCounsellorCode();
            if (StringUtils.isNotEmpty(code)) {
                SysUser sysUser = sysUserList.stream().filter(x -> x.getUserName().equals(code)).findFirst().orElse(null);
                if (ObjectUtil.isNotEmpty(sysUser)) {
                    statisticStudentBaseInfo.setCounsellorId(sysUser.getUserId());
                    statisticStudentBaseInfo.setCounsellorName(sysUser.getNickName());
                }
            }
        }
        return list;
    }
}
